#include "CSetMeshFileCommand.h"
#include "CSetAnimationFileCommand.h"
#include "CUserInterface.h"
#include "CLevelEditor.h"
#include "FpsSkyBoxSceneNode.h"
//#include "IColladaSceneNodeAnimator.h"
//#include "CColladaCameraSceneNode.h"
#include "FpsColladaDatabase.h"
#include "CSceneObject.h"

extern CGlobals* g;
extern CUserInterface* ui;
extern CLevelEditor* editor;
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
CSetMeshFileCommand::CSetMeshFileCommand (int id, stringc newMeshFile, bool create)
{
	m_id = id;
	CSceneObject* obj = editor->getSceneObjectFromID(id);	
	m_oldMeshFile = obj->m_UserData->getAttributeAsString("MeshFile");
	m_newMeshFile = newMeshFile;
	setDescription(L"change MeshFile");
	bCreateProps = create;
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void CSetMeshFileCommand::redo ()
{
	doSet(m_newMeshFile);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void CSetMeshFileCommand::undo ()
{	
	doSet(m_oldMeshFile);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void CSetMeshFileCommand::doSet(stringc mesh, bool bSetAnim)
{
	CSceneObject* obj = editor->getSceneObjectFromID(m_id);
	obj->m_UserData->setAttribute("MeshFile", mesh.c_str());		

	ISceneNode* parent = obj->m_SceneNode->getParent();

	//if (!parent)
	{	
		parent = g->sceneManager->getRootSceneNode();
	}
	IAttributes * attr = g->device->getFileSystem()->createEmptyAttributes();
	
	obj->m_SceneNode->serializeAttributes(attr);
	obj->m_SceneNode->remove();	
	

	if (mesh != "")
	{	
		if (obj->m_UserData->getAttributeAsString("!GameType") == "SkyPlane")
		{
			obj->m_SceneNode = new FpsSkyBoxSceneNode(mesh.c_str());
		}	
		//else if (obj->m_UserData->getAttributeAsString("!GameType") == "Collisions")
		//{
		//	editor->m_physicalWorld->AddCollisionRoom(mesh.c_str());
		//}
		else
		{
			//obj->m_SceneNode->removeAll();
#if 0
			obj->m_SceneNode = NULL;//ConstructColladaScene(mesh.c_str());
#else
			IAnimatedMesh* themesh = g->sceneManager->getMesh(mesh.c_str());		
			obj->m_SceneNode = g->sceneManager->addAnimatedMeshSceneNode( themesh );
#endif
		}
		
		if (!obj->m_SceneNode)
		{
			stringw strError;
			strError = L"Object ID ";
			strError += m_id;
			strError += L" cannot load MeshFile : '";
			strError += stringw(mesh.c_str());
			strError += "'!!!";			
			obj->m_SceneNode = g->sceneManager->addEmptySceneNode();
									
			//g->guienv->addMessageBox(L"Error!", strError.c_str(), false);
			DEBUG_OUT(" ERROR %s \n" ,  stringc(strError.c_str()).c_str() );
				

		}
	}
	else
	{
		obj->m_SceneNode = g->sceneManager->addEmptySceneNode();
	}
	
	editor->GetAbsoluteBoundingBox(obj->m_SceneNode, obj->m_box);
	editor->HideLods(obj->m_SceneNode);

	obj->m_SceneNode->deserializeAttributes(attr);
	attr->drop();

	obj->m_SceneNode->grab();
	parent->addChild(obj->m_SceneNode);


	if (bSetAnim && obj->m_UserData->existsAttribute("AnimationFile"))
	{
		stringc str = obj->m_UserData->getAttributeAsString("AnimationFile");
		CSetAnimationFileCommand* cmd = new CSetAnimationFileCommand(m_id, str);
		cmd->redo();
		cmd->drop();
	}

	//DEBUG_OUT("-------------------------------------------------------\n");
	//editor->PrintRelativeTrans(obj->m_SceneNode);
	if (bCreateProps)
	{
		editor->setSelectedNode(obj->m_SceneNode);
		ui->createSelectedObjectProperties();
	}

	if (obj->m_UserData->getAttributeAsString("!GameType") == "Geometry")
	{
		editor->AddLinkHelperNodes(obj->m_SceneNode);
	}	
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
